const router = require('koa-router')();
const caseService = require('../service/caseService');
const commonService = require('../service/commonService');
const getTimeHelper = require("../util/getTimeHelper");
router.prefix('/api/case');

/**
 * @api {post} /api/case/addCase 添加案例
 * @apiName addCase
 * @apiGroup case
 *
 * @apiParam {String} name 案例名称.
 * @apiParam {String} type 案例分类（1：现代化办公室，2：高端写字楼，3：高端别墅）.
 * @apiParam {bool}  recommend 是否推荐（true,false）.
 * @apiParam {String} time 案例完成时间.
 * @apiParam {String} url 全景图链接.
 * @apiParam {String} mainImg 主图url.
 * @apiParam {String} introduce 案例介绍.
 *
 * @apiSuccess {int} code 返回状态码（1：添加成功，2：添加失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/addCase', async (ctx) => {
    try {
        let data = ctx.request.body;
        let name = data.name,
            type = data.type,
            recommend = data.recommend,
            time = data.time,
            url = data.url,
            mainImg = data.mainImg,
            introduce = data.introduce;
        if (!name || !type || !time || !url || url.length <= 0 || !mainImg || !introduce) {
            let data = {code: 1, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        if (recommend === "true") {
            data.recommend = '1';
        } else {
            data.recommend = '0';
        }
        //let tempTime=getTimeHelper.getTime(time);
        //data.time=tempTime;
        let result;
        result = await caseService.handleAddCase(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/case/editCase 修改案例
 * @apiName editCase
 * @apiGroup case
 *
 *
 * @apiParam {String} id 案例id.
 * @apiParam {String} name 案例名称.
 * @apiParam {String} type 案例分类（1：现代化办公室，2：高端写字楼，3：高端别墅）.
 * @apiParam {bool}  recommend 是否推荐（true,false）.
 * @apiParam {String} time 案例完成时间.
 * @apiParam {String} url 全景图链接.
 * @apiParam {String} mainImg 主图url.
 * @apiParam {String} introduce 案例介绍.
 *
 * @apiSuccess {int} code 返回状态码（1：添加成功，2：添加失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/editCase', async (ctx) => {
    try {
        let data = ctx.request.body;
        let name = data.name,
            type = data.type,
            recommend = data.recommend,
            time = data.time,
            url = data.url,
            mainImg = data.mainImg,
            introduce = data.introduce,
            id = data.id;
        if (!name || !type || !time || !url || url.length <= 0 || !mainImg || !introduce || !id) {
            let data = {code: 1, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        if (recommend === "true") {
            data.recommend = '1';
        } else {
            data.recommend = '0';
        }

        let result;
        result = await caseService.handleEditCase(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});


/**
 * @api {post} /api/case/getCase 获取案例列表
 * @apiName getCase
 * @apiGroup case
 *
 * @apiParam {String} name 案例名称.（当参数全部为空时则获取全部数据）
 * @apiParam {String} type 案例分类（1：现代化办公室，2：高端写字楼，3：高端别墅）.
 *
 * @apiSuccess {int} code 返回状态码（1：获取成功，2：没有数据，3：服务器出错）
 * @apiSuccess {String} msg 返回状态码对应的信息
 * @apiSuccess {Object[]} data 获取成功时返回的数据
 * @apiSuccess {String} data.case_id 案例id
 * @apiSuccess {String} data.case_name 案例名称
 * @apiSuccess {String} data.case_type 案例类别
 * @apiSuccess {String} data.is_recommend 是否推荐（0：否，1：是）
 * @apiSuccess {time} data.achievement_time 案例完成时间
 * @apiSuccess {String} data.case_url 全景图链接
 * @apiSuccess {String} data.case_mainImg 主图url
 * @apiSuccess {String} data.case_introduce 案例介绍
 * @apiSuccess {String} data.is_delete 标识是否删除（0：否，1：是）
 * @apiSuccess {String} data.is_enabled 标识是否启用（0：否，1：是）
 */

router.post('/getCase', async (ctx) => {
    try {
        let data = ctx.request.body;
        let name = data.name,
            type = data.type;
        if (!name && !type) {
            data = null;
        }
        let result;
        result = await caseService.handleSearchCase(data);
        let len = result.data.length;
        let tempData = result.data;
        for (let i = 0; i < len; i++) {
            let achievement_time = getTimeHelper.getTime(tempData[i].achievement_time);
            result.data[i].achievement_time = achievement_time;
        }
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});
module.exports = router;

/**
 * @api {post} /api/case/deleteCase 删除案例
 * @apiName deleteCase
 * @apiGroup case
 *
 * @apiParam {String} case_id 案例id.
 *
 * @apiSuccess {int} code 返回状态码（1：删除成功，2：删除失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 */

router.post('/deleteCase', async (ctx) => {
    try {
        let data = ctx.request.body;
        let id = data.case_id;
        if (!id) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
        }
        let result, result1;
        let checkData = {};
        checkData.id = data.case_id;
        result1 = await commonService.handleCheckRelate(checkData);
        if (result1.code == 1) {
            data.is_relate = 1;
        } else {
            data.is_relate = 0;
        }
        ;
        result = await caseService.handleDeleteCase(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/case/checkCase 删除前判断当前案例是否被广告使用
 * @apiName checkCase
 * @apiGroup case
 *
 * @apiParam {String} id 案例id.
 *
 * @apiSuccess {int} code 返回状态码（1：已被使用，2：未被使用，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/checkCase', async (ctx) => {
    try {
        let data = ctx.request.body;
        let id = data.id;
        if (!id) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        let result;
        result = await commonService.handleCheckRelate(data);
        if (result.code == 1) {
            result.msg = '已被使用';
        } else if (result.code == 2) {
            result.msg = '未被使用';
        }
        result.data = undefined;
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/case/startCase 启用案例
 * @apiName startCase
 * @apiGroup case
 *
 * @apiParam {String} case_id 案例id.
 *
 * @apiSuccess {int} code 返回状态码（1：启用成功，2：启用失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 */

router.post('/startCase', async (ctx) => {
    try {
        let data = ctx.request.body;
        let id = data.case_id;
        if (!id) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
        }
        let result, result1;
        let checkData = {};
        checkData.id = data.case_id;
        result1 = await commonService.handleCheckRelate(checkData);
        if (result1.code == 1) {
            data.is_relate = 1;
        } else {
            data.is_relate = 0;
        }
        ;
        result = await caseService.handleStartCase(data);
        if (result.code === 1) {
            result.msg = '启用成功！';
        } else if (result.code === 2) {
            result.msg = '启用失败！';
        }
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/case/outageCase 停用案例
 * @apiName outageCase
 * @apiGroup case
 *
 * @apiParam {String} case_id 案例id.
 *
 * @apiSuccess {int} code 返回状态码（1：停用成功，2：停用失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 */

router.post('/outageCase', async (ctx) => {
    try {
        let data = ctx.request.body;
        let id = data.case_id;
        if (!id) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
        }
        let result, result1;
        let checkData = {};
        checkData.id = data.case_id;
        result1 = await commonService.handleCheckRelate(checkData);
        if (result1.code == 1) {
            data.is_relate = 1;
        } else {
            data.is_relate = 0;
        }
        result = await caseService.handleOutageCase(data);
        if (result.code === 1) {
            result.msg = '停用成功！';
        } else if (result.code === 2) {
            result.msg = '停用失败！';
        }
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});
module.exports = router;